package com.tiancheng.trade.settlement.util;

import com.tiancheng.trade.commom.core.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Csv工具
 */
@Slf4j
public class CsvUtils {

    /**
     * 读取CSV文件
     *
     * @param filePath    CSV文件的绝对文件路径
     * @param skipHeader  是否跳过表头所在的行
     * @param charsetName 编码
     * @return
     * @throws IOException
     */
    public static Object[][] getData(String filePath, boolean skipHeader, String charsetName) {
        List<String[]> records = new ArrayList<String[]>();
        String record;
        try {
            BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), charsetName));
            if (skipHeader) {
                file.readLine();
            }
            while ((record = file.readLine()) != null) {
                String fields[] = record.split(",");
                records.add(fields);
            }
            // 关闭文件
            file.close();
        } catch (Exception ex) {
            log.error("CsvUtils getData 异常" + ex.getMessage());
        }

        // 将存储测试数据的List转换为一个Object的二维数组
        Object[][] results = new Object[records.size()][];
        // 设置二位数组每行的值，每行是一个Object对象
        for (int i = 0; i < records.size(); i++) {
            results[i] = (Object[]) records.get(i);
        }
        return results;
    }

    /**
     * 获取String,替换`为空
     *
     * @param value 字符串
     * @return
     */
    public static String GetString(String value) {
        if (value != null) {
            value = value.replace("`", "");
            value = value.trim();
        }
        return value;
    }

    /**
     * 获取LocalDate
     *
     * @param date 字符串
     * @return
     */
    public static LocalDate GetLocalDate(String date) {
        date = GetString(date);
        if (StringUtils.isEmpty(date)) {
            return null;
        } else {
            LocalDate localDate = DateTimeUtil.formatDate(date, "yyyyMMdd");
            return localDate;
        }
    }

    /**
     * 获取Date
     *
     * @param strDate 字符串
     * @param pattern 格式
     * @return
     */
    public static Date getDate(String strDate, String pattern) {
        strDate = GetString(strDate);
        if (StringUtils.isEmpty(strDate)) {
            return null;
        }
        Date date = null;
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            date = new Date(dateFormat.parse(strDate).getTime());
        } catch (ParseException e) {
            log.error("CsvUtils getDate 异常 " + e.getMessage());
        }
        return date;
    }

    /**
     * 获取LocalDateTime
     *
     * @param dateTime 字符串
     * @return
     */
    public static LocalDateTime GetLocalDateTime(String dateTime) {
        return GetLocalDateTime(dateTime, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取LocalDateTime
     *
     * @param dateTime 字符串
     * @return
     */
    public static LocalDateTime GetLocalDateTime(String dateTime, String pattern) {
        dateTime = GetString(dateTime);
        if (StringUtils.isEmpty(dateTime)) {
            return null;
        } else {
            LocalDateTime localDalteTime = getDate(dateTime, pattern).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            return localDalteTime;
        }
    }

    /**
     * 获取金额，元转分
     *
     * @param amount 字符串
     * @return
     */
    public static Integer GetAmount(String amount) {
        BigDecimal bigDecimal100 = new BigDecimal(100);
        amount = GetString(amount);
        if (StringUtils.isEmpty(amount)) {
            return null;
        } else {
            return new BigDecimal(amount).multiply(bigDecimal100).intValue();
        }
    }

    /**
     * 获取BigDecimal
     *
     * @param value 字符串
     * @return
     */
    public static BigDecimal getBigDecimal(String value) {
        value = GetString(value);
        if (StringUtils.isEmpty(value)) {
            return null;
        } else {
            return new BigDecimal(value);
        }
    }

    /**
     * 保存csv文件
     *
     * @param filePath 文件全路径
     * @param str      字符串
     * @throws IOException
     */
    public static void writeFile(String filePath, String str) throws IOException {
        OutputStream os = new FileOutputStream(filePath);
        PrintWriter pw = new PrintWriter(os);
        pw.print(str);
        pw.close();
        os.close();
    }

}
